import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    //注册
    return ChangeNotifierProvider(
      create: (BuildContext context) => LikesModel(),
      child: Scaffold(
      body: MyLikesPage(),
      appBar: AppBar(
        title: Text("Life Cycle 生命周期"),
       ),
     ),
   );

    
  }
}

class MyLikesPage extends StatelessWidget {
  const MyLikesPage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Text('${context.watch<LikesModel>().counter}'),
          TextButton(onPressed: Provider.of<LikesModel>(context).incrementCounter, 
          child: Icon(Icons.thumb_up))
        ],
      ),
    );
  }
}


//
class LikesModel extends ChangeNotifier {
  int _counter = 0;
  int get counter => _counter;

  incrementCounter() {
    _counter++;

    //通知更新
    notifyListeners();
  }
}
